package upm.pb4agent.team3;

import jade.core.Agent;
import jade.util.leap.Properties;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;


/**
 * This agent has the following functionality: 
 * <ul>
 * <li> 
 * <li> 
 * </ul>
 * @author Team3, UPM
 */
public final class ReadConfiguration extends Agent{
	private static final long serialVersionUID = -2969783777924732092L;
	private static URL url = null;
	private String directory = "";  
	private String numberOfFiles = "";  
	private String fileExtension = ""; 
	private static Properties  prop = new Properties();  
	private static ReadConfiguration readConfiguration = null;
	private static ReadConfiguration readConfigurationLocal = null;
	
	public ReadConfiguration(String name){  
		try{
			/*InputStream is = ReadConfiguration.class.getResourceAsStream(Constant.PROP_FILE);
			System.out.println(Constant.NAME_AG_PLATFORM_X + ": " + url.getFile() + Constant.PROP_FILE);*/
			
			url = ReadConfiguration.class.getProtectionDomain().getCodeSource().getLocation();
			String ruta = url.getFile() + name;//"upm/pb4agent/config.properties";
			InputStream is = new FileInputStream(ruta);
			System.out.println(Constant.NAME_AG_PLATFORM_X + ": " + ruta);
			
			prop.load(is);  
			directory = prop.getProperty("Directory");  
			numberOfFiles = prop.getProperty("NumberOfFiles");  
			fileExtension = prop.getProperty("Extension");  
			is.close();
		}catch(Exception e){  
			System.out.println(Constant.NAME_AG_PLATFORM_X + ".ReadConfiguration(): +  Failed to read from " + Constant.PROP_FILE + " file.");  
		}  
	}  
	 
	public static ReadConfiguration getInstance(int type){  
	       try{
	    	   if(type == 0){
		    	   if(readConfiguration == null){	    		   
						readConfiguration = new ReadConfiguration(Constant.PROP_FILE);
					}
		    	   return readConfiguration;
	    	   }else{
	    		   if(readConfigurationLocal == null){	    		   
	    			   readConfigurationLocal = new ReadConfiguration(Constant.PROP_FILE_LOCAL);
					}
		    	   return readConfigurationLocal;
	    	   }
		   }catch(Exception e){  
		     System.out.println(Constant.NAME_AG_PLATFORM_X + "ReadConfiguration.getInstance(): +  Failed to read from " + Constant.PROP_FILE + " file.");
		     return null;
	       }  
	}  
	

	 public static String getValue(String propertie){
		 try{			 
			 return prop.getProperty(propertie);
		 }catch(Exception e){  
		     System.out.println(Constant.NAME_AG_PLATFORM_X + "ReadConfiguration.getValue(): + Failed to read from " + Constant.PROP_FILE + " file.");
		     return null;
	     }  		 
	 }  
	 
}
